using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Ro.Norc.ServiceStackClientCustomEncoderSample.ServiceModel.Operations;
using Ro.Norc.ServiceStackClient;

namespace Ro.Norc.ServiceStackClientCustomEncoderSample
{
	public class HelloViewController : UIViewController
	{
		private Service<HelloResponse> mService = null;
		
		private UITextField mFirstNameTxt = null;
		
		private UITextField mLastNameTxt = null;
		
		private UIButton mSubmitBtn = null;
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			
			//setup service
			mService = new Service<HelloResponse>("http://tests.formSubmit/ProcessForm.ashx");
			mService.Encoder = new ServiceEncoderForm();
			mService.OptAutoAppendEndpointParts = false;
			mService.OptAutoAppendRequestName = false;
			mService.ServiceResult += delegate(object sender, ServiceResultArgs<HelloResponse> args) {
				UIAlertView av = new UIAlertView("Result", args.Result.Result, null, "Ok", null);
				av.Show();
			};
			mService.ServiceError += delegate(object sender, ServiceErrorArgs args) {
				UIAlertView av = new UIAlertView("Result", "An error occured", null, "Ok", null);
				av.Show();
			};
			
			//setup form
			mFirstNameTxt = new UITextField();
			mFirstNameTxt.BorderStyle = UITextBorderStyle.RoundedRect;
			mFirstNameTxt.Frame = new RectangleF(10, 10, View.Frame.Width - 20, 35);
			mFirstNameTxt.Font = UIFont.SystemFontOfSize(14);
			
			mLastNameTxt = new UITextField();
			mLastNameTxt.BorderStyle = UITextBorderStyle.RoundedRect;
			mLastNameTxt.Frame = new RectangleF(10, mFirstNameTxt.Frame.Y + mFirstNameTxt.Frame.Height + 10, View.Frame.Width - 20 , 35);
			mLastNameTxt.Font = UIFont.SystemFontOfSize(14);
			 
			mSubmitBtn = UIButton.FromType(UIButtonType.RoundedRect);
			mSubmitBtn.SetTitle("Submit form", UIControlState.Normal);
			mSubmitBtn.Frame = new RectangleF(10, mLastNameTxt.Frame.Y + mLastNameTxt.Frame.Height + 10, View.Frame.Width - 20, 30);
			mSubmitBtn.TouchDown += delegate {
				mService.Send (new Hello() {
					FirstName = mFirstNameTxt.Text,
					LastName = mLastNameTxt.Text
				});
			};
			
			//add subviews
			View.AddSubview(mFirstNameTxt);
			View.AddSubview(mLastNameTxt);
			View.AddSubview(mSubmitBtn);
			
			View.BackgroundColor = UIColor.White;
		}
	}
}